//index.js
//获取应用实例
import Api, { img_root, GET_PIC_WALL, GET_HOME_CATE, GET_CUR_TIME_BUY, GET_TIME_BUY_LIST_, GET_TIME_BUY_GOODS_LIST,GET_HOME_GOODS } from '../../utils/api';
import { handPic,timeFormat,getUrl} from '../../utils/util';

const App = getApp();

Page({
    data: {
        swiper: [],
        timebuy: false,
        keyword: '',
        count_down: [],
        cate: [],
        goods_list:[]
    },
    //事件处理函数
    bindViewTap: function () {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },

    // 通用页面跳转
    navigateTo: function (e) {
        let url;
        const {tip,idx} = e.currentTarget.dataset;

        const {keyword } = this.data;
     
        if (tip == 'goods_detail') {
            url = getUrl(tip,{gc_id:idx});
        } else if (tip == 'search') {
            url = `../search/search?keyword=${keyword}`;
        } else if (tip == 'notification') {
            url = `../mine/notification/notification`;
        } else if (tip == 'cate') {
            // 设置参数 具体分类 data-
            url = getUrl(tip,{cateid:idx});
        } else if (tip == 'panic') {
            url = `../special_offer/special_offer`;
        }
        
        App.navigateTo(url)

    },
    onLoad: function () {
        const that = this;
        //  ============================  获取首页数据 =======================
        // 轮播图
        Api.fetch(GET_PIC_WALL, {}, (res) => {
            res.map((item, idx) => {
                item.logo = handPic(item.logo, 4)
            });
            this.setData({
                swiper: res
            });
        });
        // 首页分类 GET_HOME_CATE
        Api.request(GET_HOME_CATE, {}, (res) => {
            res.map((val, key) => {
                val.map((v, k) => {
                    v.img = handPic(v.img, 4)
                })
            });
            this.setData({
                cate: res
            })
        });
        // 当前抢购商品 GET_CUR_TIME_BUY
        Api.request(GET_CUR_TIME_BUY, {}, (res) => {
            if(res){
                
                let over_time = parseInt(res.ctime) + 3600;
                let current_time = res.curtime;
                res.format = timeFormat(res.ctime,'hi');
                // 倒计时 
                if (over_time > current_time) {
                    let { h, m, s } = App.count_down(over_time, current_time += 1);
                    that.setData({ count_down: [h, m, s] })
                    this.clear = setInterval(() => {
                        const { h, m, s } = App.count_down(over_time, current_time += 1);
                        if (h == "00" && m == "00" && s == "00") {
                            clearInterval(this.clear);
                        }
                        this.setData({ count_down: [h, m, s] })
                    }, 1000)
                } else {
                    // 这是没有的
                    this.setData({ count_down: ["00", "00", "00"] })
                }

                res.goods.map((v,k)=>{
                    v.default_img = handPic(v.default_img,4);
                })
                
                this.setData({
                    timebuy: res
                })
            }
        });
        //首页商品列表
        Api.fetch(GET_HOME_GOODS,{},resp => {
            resp.map((v,k) => {
                v.default_img = handPic(v.default_img,8);
            })

            this.setData({
                goods_list:resp
            })
        })

    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function () {
        
    },

})
